package com.mz.mattress.admin.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mz.mattress.admin.api.resp.IntegerCount;
import com.mz.mattress.admin.api.resp.MerchantDeviceSum;
import com.mz.mattress.api.entity.Device;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface DeviceMapper extends BaseMapper<Device> {

    Page<Device> listPage(Page page,
                          @Param("id") Integer id,
                          @Param("province") String province,
                          @Param("city") String city,
                          @Param("district") String district,
                          @Param("memberId") Long memberId,
                          @Param("merchantId") Integer merchantId,
                          @Param("merchantName") String merchantName,
                          @Param("saleId") Integer saleId,
                          @Param("topSaleId") Integer topSaleId,
                          @Param("name") String name,
                          @Param("deviceCode") String deviceCode,
                          @Param("status") Integer status,
                          @Param("online") Integer online,
                          @Param("startTime") String startTime,
                          @Param("endTime") String endTime);

    Page<Device> listPageMerchant(Page page,
                                  @Param("memberId") Long memberId,
                                  @Param("merchantId") Integer merchantId,
                                  @Param("saleId") Integer saleId,
                                  @Param("topSaleId") Integer topSaleId);


    @Select("select count(*) from mz_device where device_code=#{deviceCode} and deleted=0")
    Integer selectCountByDeviceCode(@Param("deviceCode") String deviceCode);

    @Select("select * from mz_device where device_code=#{deviceCode} and deleted=0")
    Device selectByDeviceCode(@Param("deviceCode") String deviceCode);

    @Update("update mz_device set merchant_id=0,merchant_name='平台',top_sale_id=null,sale_id=null where id=#{id}")
    Integer clearMerchantId(@Param("id") Integer id);

    @Select("select * from mz_device where merchant_id=#{merchantId} and deleted=0")
    List<Device> selectByMerchantId(@Param("merchantId") Integer merchantId);

    @Select("select count(*) from mz_device where merchant_id=#{merchantId} and deleted=0")
    Integer selectCountByMerchantId(@Param("merchantId") Integer merchantId);

    List<IntegerCount> selectSumByColumn(@Param("columnName") String columnName, @Param("columnValue") Integer columnValue);

    List<IntegerCount> selectSumByMemberId(@Param("memberId") Long memberId);

    @Select("select merchant_id merchantId,count(*) as count from mz_device where deleted=0 group by merchant_id")
    List<MerchantDeviceSum> sumDevice();

    @Update("update mz_device set status=0,work_status=0,online=1 where id=#{id}")
    Integer resetCanUse(@Param("id") Integer id);

    @Update("update mz_device set status=0,work_status=0,online=1 where merchant_id=#{merchantId}")
    Integer resetCanUseByMerchantId(@Param("merchantId") Integer merchantId);

    @Select("select id from mz_device where deleted=0 and demo=1")
    List<Integer> demoList();

//    @Select("select id from mz_device where deleted=0 and isnull(demo)")
    @Select("select id from mz_device where deleted=0 order by id asc ")
    List<Integer> deviceList();

}
